export default [{
  Page: {
    onLoad() {
      bindSetData(this)
      // 重写setData方法
      this.superSetData = this.setData
      this.setData = (dataOptions) => {
        // 1.传递参数到子组件
        let childrenComponents = this.ak_components
        // console.log("赋值组件", this.ak_components)
        if (childrenComponents) {
          for (let item of childrenComponents) {
            if (item.data.vModel) {
              item.setData({
                value: dataOptions[item.data.vModel]
              })
            }
          }
        }
        // 2.新增劫持参数
        for (let key in dataOptions) {
          if (!(key in this.data)) {
            defineProperty(this, key)
          }
        }
        // 调用原生方法
        this.superSetData(dataOptions)
      }
    },
    inputData(e) {
      const {
        name
      } = e.currentTarget.dataset
      const {
        value
      } = e.detail
      console.log("内容", value)
      this[name] = value
    }
  },
  Component:{
    created(){
      bindSetData(this)
      // 重写setData方法
      this.superSetData = this.setData
      this.setData = (dataOptions) => {
        // 1.新增劫持参数
        for (let key in dataOptions) {
          if (!(key in this.data)) {
            defineProperty(this, key)
          }
        }
        // 调用原生方法
        this.superSetData(dataOptions)
      }
    }
  }
}]


function bindSetData(that) {
  // if(that.isDefineProperty) return
  // 定义空对象
  for (let key in that.data) {
    defineProperty(that, key)
  }
  // that.isDefineProperty = true
}

// 劫持数据
function defineProperty(that, key) {
  // 1.注入到当前页面
  Object.defineProperty(that, key, {
    get() {
      // console.log("读取数值")
      return that.data[key]
    },
    set(val) {
      // console.log("写入数值")
      const params = {}
      // 2.替换data值
      params[key] = val
      that.setData(params)
    }
  })
}